// var rootUrl = "capi/V2/permissionV2/memberUser/list" 现版本接口
var rootUrl = "capi/permission/memberUser/"
var storageName = "userInfo";
var wx_synchronization_flag = 0;
var netTool
// var checkdata = require('../utils/dataCheek')
var page = {
	pageSize: 10,
	total: 0,
	currentPage: 1
}
/**
 * wx_synchronization_flag = 1表示已经拉取用户信息
 */
function UserInfo(_netTool) {
	netTool = _netTool
	// 用户登录
	/**
	 * @param {Object} data
	 * open_id_wx 唯一标识
	 * 
	 * 
	 * @param {Object} fn
	 */
	//types  1 驾校  2 教练  3 区域代理  4 经理v  5 学员 capi/wcoa/login
	this.wxlogin = function(data, fn) {
		var that = this; 
		netTool.getAjaxUrlValue('capi/wcoa/login', { 
				config_business_id:'1028_2',
				...data,
			},  res =>{  
				typeof fn == 'function' && fn(res);
			})
	}
	this.login = function(data, fn) { 
		console.log(1111)
		var that = this; 
		netTool.getAjaxUrlValue(rootUrl + 'login', { // mark: xxx
				...data
			},  res =>{  
				if(res.records > 0){
					console.log(res)
					res.login = true
					// 登录成功  
					typeof fn == 'function' && fn(res);
				}else { 
					uni.$u.toast('账号密码错误')
				 typeof fn == 'function' && fn({success:false});
				 
				}
			})
	}
	this.list = function(data, fn) {
		netTool.getAjaxUrlValue(rootUrl + 'list', {
			...data
		}, function(res) {
			typeof fn == 'function' && fn(res);
		})
	}
	this.restPassword = function(data, fn) {
		netTool.getAjaxUrlValue( 'capi/V2/coreAccount/memberUser/resetPassword', {
			...data
		}, function(res) {
			typeof fn == 'function' && fn(res);
		})
	}
	this.save = function(data, fn) {
		uni.showLoading({
			title:'正在注册...'
		})
		netTool.getAjaxUrlValue(rootUrl + 'save', {
			...data
		}, function(res) {
			console.log('注册',res)
			netTool.getAjaxUrlValue(rootUrl + 'findById',{
				id:res.id, 
			},res => {
				console.log('获取',res)
			 uni.hideLoading()
			  typeof fn == 'function' && fn(res);
			})
			 
		})
	}
	  this.get = function() {
		return uni.getStorageSync('userInfo')
	  }
	this.bindMobile = function(data,fn) {
		
		netTool.getAjaxUrlValue('capi/V2/permissionV2/memberUser/bindMobileById',{
			...data
		},res => { 
		 if(!uni.getStorageSync('userInfo').id) return
		 	netTool.getAjaxUrlValue(rootUrl + 'findById', {
		 	id: uni.getStorageSync('userInfo').id,
		 	...data 
		 }, function(res) { 
			uni.setStorageSync('userInfo',res.rows)
		 })
		  typeof fn == 'function' && fn(res);
		})
	}
	this.update = function(data, fn) {
		console.log('进入更新')
		if (data.real_name == 'undefined') {
			data.real_name = ''
		}
		var that = this;
		netTool.getAjaxUrlValue(rootUrl + 'update', data, function(res) {
			
			let _chache = that.get()
			_chache.id
			if (_chache) {
				let _cache1 = {
					id: _chache.id,
					..._chache,
					...data
				}

				uni.setStorageSync('userInfo', _cache1)
				_chache = that.get()
				typeof fn == "function" && fn(_chache);
			} else {
				netTool.getAjaxUrlValueBackground(rootUrl+'list', {
					id: data.id
				}, function(res) {
					uni.setStorageSync('userInfo', res.rows[0])
					typeof fn == "function" && fn(res.rows[0]);
				})
			}
		})
	}
	this.getInfo = function(data, fn) {
		if(!uni.getStorageSync('userInfo').id) return
		netTool.getAjaxUrlValue(rootUrl + 'findById', {
		id: uni.getStorageSync('userInfo').id,
		...data 
	}, function(res) {
		console.log('222222222222222222222',res)
		typeof fn == 'function' && fn(res);
	})
	}
 
 
 
}
module.exports = UserInfo;
